[#ftl]
[#import "common/macrolibs.ftl" as lib]
[#import "common/commondb.ftl" as db]
[#import "common/commonclass.ftl" as cls]
<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${lib.packageBase}.mappers.${cls.mySimpleClassName}Mapper">
    <!-- 新增 -->
    <insert id="insert" parameterType="${cls.myFullClassName}">
        INSERT INTO ${db.myTableName}(name,description, disabled[#if 
        clazz.ownerLevel>0], ownerid[/#if][#if
        clazz.scopeClass??], scopeid[/#if][#list
        clazz.dynaAttributes as attr][#if !attr.carrier], ${db.myFieldNames["${attr.id}"]}[/#if][/#list])
        VALUES (${r"#{name},#{description},#{disabled}"}[#if 
        clazz.ownerLevel>0]${r", #{owner.id}"}[/#if][#if
        clazz.scopeClass??]${r", #{scope.id}"}[/#if][#list
        clazz.dynaAttributes as attr][#if !attr.carrier], ${r"#{"}${db.myFieldValues["${attr.id}"]}}[/#if][/#list])
        <selectKey keyProperty="id" resultType="int" order="AFTER">
			select @@IDENTITY as id
		</selectKey> 
    </insert>
    
    <!-- 修改 -->
    <update id="update" parameterType="${cls.myFullClassName}">
        update ${db.myTableName}
        set name=${r"#{name}"},
            description=${r"#{description}"},
            disabled=${r"#{disabled}"}[#list
        clazz.dynaAttributes as attr][#if !(attr.carrier||attr.eternal)], 
            ${db.myFieldNames["${attr.id}"]}=${r"#{"}${db.myFieldValues["${attr.id}"]}}[/#if][/#list]
        where id=${r"#{id}"}
    </update>
    
    <resultMap type="${cls.myFullClassName}" id="${cls.myModuleName}Map">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="description" column="description"/>
        <result property="disabled" column="disabled"/>
        [#list clazz.dynaAttributes as attr][#if !attr.carrier][#if attr.type<9]
        <result property="${db.myAttrNames["${attr.id}"]}" column="${db.myFieldValues["${attr.id}"]}"/>
        [/#if][/#if][/#list]
        <!--result property="checked" column="checked"/-->
        [#if clazz.ownerLevel>0]
        <association property="owner" javaType="com.smzd.framework.system.domain.[@lib.ownerClassName level=clazz.ownerLevel/]">
            <id property="id" column="ownerid"/>
            <result property="name" column="ownername"/>
        </association>
        [/#if][#if clazz.scopeClass??]
        <association property="scope" javaType="[@lib.fullClassName clazz=clazz.scopeClass/]">
            <id property="id" column="scopeid"/>
            <result property="name" column="scopename"/>
        </association>
        [/#if]
        [#list clazz.dynaAttributes as attr][#if !attr.carrier][#if attr.type>8]
        <association property="${db.myAttrNames["${attr.id}"]}" javaType="[@lib.attrClassName attr=attr/]">
            <id property="id" column="${db.myFieldNames["${attr.id}"]}"/>
            <result property="name" column="${db.myAttrNames["${attr.id}"]}name"/>
        </association>
        [/#if][/#if][/#list]
    </resultMap>
    
    <!-- 分页查询 -->
    <select id="getElemsByPage" resultMap="${cls.myModuleName}Map" parameterType="com.smzd.core.utils.db.${cls.myPageName}">
    select * from (
		SELECT TOP (${r"${pageSize}"}+${r"${offset}"}) row_number() over (order by ${db.myTableAlias}.${r"${sortName} ${sortOrder}"}, ${db.myTableAlias}.id)as _num_, ${db.myTableAlias}.* [#if 
        clazz.ownerLevel>0], owner.name ownername[/#if][#if
        clazz.scopeClass??], scope.name scopename[/#if][#list clazz.dynaAttributes as attr][#if !attr.carrier][#if
        attr.type>8], ${db.myRefTableAlias["${attr.id}"]}.name ${db.myAttrNames["${attr.id}"]}name[/#if][/#if][/#list]
        FROM ${db.myTableName} ${db.myTableAlias}
         [#if clazz.ownerLevel>0]
        left join ${db.myOwnerTableName} owner
        on owner.id=${db.myTableAlias}.ownerid
        [/#if][#if clazz.scopeClass??]
        left join ${db.myScopeTableName} scope
        on scope.id=${db.myTableAlias}.scopeid
        [/#if][#list clazz.dynaAttributes as attr][#if !attr.carrier][#if attr.type>8]
        left join ${db.myRefTables["${attr.id}"]} ${db.myRefTableAlias["${attr.id}"]}
        on ${db.myRefTableAlias["${attr.id}"]}.id=${db.myTableAlias}.${db.myFieldNames["${attr.id}"]}
        [/#if][/#if][/#list][#if clazz.scopeClass??]
        where ${db.myTableAlias}.scopeid=${r"#{scope.id}"}
        [#if clazz.ownerLevel>0]
        and ${db.myTableAlias}.ownerid=${r"#{owner.id}"}
        [/#if][#else][#if clazz.ownerLevel>0]
        where ${db.myTableAlias}.ownerid=${r"#{owner.id}"}
        [/#if][/#if]
        order by ${db.myTableAlias}.${r"${sortName} ${sortOrder}"}
	) as _t_
	where _t_._num_>${r"${offset}"}
    </select>
    
    <!-- 分页查询数量 -->
    <select id="getElemsCount" resultType="int" parameterType="com.smzd.core.utils.db.${cls.myPageName}">
        select
            count(*) c
        from 
            ${db.myTableName} ${db.myTableAlias}
        [#if clazz.scopeClass??]
        where ${db.myTableAlias}.scopeid=${r"#{scope.id}"}
        [#if clazz.ownerLevel>0]
        and ${db.myTableAlias}.ownerid=${r"#{owner.id}"}
        [/#if][#else][#if clazz.ownerLevel>0]
        where ${db.myTableAlias}.ownerid=${r"#{owner.id}"}
        [/#if][/#if]
    </select>
    
    <select id="getElemById" resultMap="${cls.myModuleName}Map" parameterType="int">
        select ${db.myTableAlias}.*[#if 
        clazz.ownerLevel>0], owner.name ownername[/#if][#if
        clazz.scopeClass??], scope.name scopename[/#if][#list clazz.dynaAttributes as attr][#if !attr.carrier][#if
        attr.type>8], ${db.myRefTableAlias["${attr.id}"]}.name ${db.myAttrNames["${attr.id}"]}name[/#if][/#if][/#list]
        from ${db.myTableName} ${db.myTableAlias}[#if clazz.ownerLevel>0]
        left join ${db.myOwnerTableName} owner
        on owner.id=${db.myTableAlias}.ownerid
        [/#if][#if clazz.scopeClass??]
        left join ${db.myScopeTableName} scope
        on scope.id=${db.myTableAlias}.scopeid
        [/#if][#list clazz.dynaAttributes as attr][#if !attr.carrier][#if attr.type>8]
        left join ${db.myRefTables["${attr.id}"]} ${db.myRefTableAlias["${attr.id}"]}
        on ${db.myRefTableAlias["${attr.id}"]}.id=${db.myTableAlias}.${db.myFieldNames["${attr.id}"]}
        [/#if][/#if][/#list]
        where ${db.myTableAlias}.id=${r"#{id}"}
    </select>
    
    <!-- 根据ID删除 -->
    <delete id="remove" parameterType="${cls.myFullClassName}">
        delete ${db.myTableAlias} from ${db.myTableName} ${db.myTableAlias}
        where ${db.myTableAlias}.id=${r"#{id}"}
    </delete>
    <!-- 扩展功能 -->

</mapper> 